<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>维修人员管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">身份证</label>
                            <div class="layui-input-inline">
                                <input type="text" name="idCard" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="repairMan-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>
        <table class="layui-hide" id="repairManTable" lay-filter="repairManTableFilter"></table>
        <script type="text/html" id="repairManTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!-- 编辑/添加弹窗 -->
        <div id="editRepairMan" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="editRepairManForm">
                <div class="layui-form-item" id="repairManId-laylabel">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="id" placeholder="自动生成" readonly value="" class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item" id="repairManName-laylabel">
                    <label class="layui-form-label required">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="repairManPhone-laylabel">
                    <label class="layui-form-label required">手机号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" lay-verify="required|phone" lay-reqtext="手机号不能为空" placeholder="请输入手机号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="repairManIdCard-laylabel">
                    <label class="layui-form-label required">身份证</label>
                    <div class="layui-input-inline">
                        <input type="text" name="idCard" lay-verify="required|identity" lay-reqtext="身份证不能为空" placeholder="请输入身份证号" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveRepairManBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        
        // 全局设置ajax请求头
        $.ajaxSetup({
            beforeSend: function(xhr) {
                const data = JSON.parse(localStorage.getItem('user'));
                if (data && data.token) {
                    xhr.setRequestHeader('Authorization', 'Bearer ' + data.token);
                }
            }
        });

        table.render({
            elem: '#repairManTable',
            url: '/repairMan/query',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', width: 120, title: '姓名'},
                {field: 'phone', width: 150, title: '手机号'},
                {field: 'idCard', width: 200, title: '身份证'},
                {title: '操作', minWidth: 150, toolbar: '#repairManTableBar', align: "center", fixed: 'right'}
            ]],
            limits: [5, 10, 20, 50],
            limit: 10,
            page: true,
            skin: 'line',
        });

        // 监听搜索操作
        form.on('submit(repairMan-search-btn)', function (data) {
            table.reload('repairManTable', {
                page: { curr: 1 },
                where: data.field
            });
            return false;
        });

        // 工具栏监听 - 添加
        table.on('toolbar(repairManTableFilter)', function (obj) {
            if (obj.event === 'add') {
                // 显示需要显示的字段
                $("#repairManId-laylabel,#repairManName-laylabel,#repairManPhone-laylabel,#repairManIdCard-laylabel").show();

                // 清空表单数据
                form.val("editRepairManForm", {
                    id: '',
                    name: '',
                    phone: '',
                    idCard: ''
                });

                // 打开弹窗
                index = layer.open({
                    title: '添加维修人员',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['500px', '400px'],
                    content: $("#editRepairMan")
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('repairManTable');
                var data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('请选择要删除的数据');
                    return;
                }
                layer.confirm('确定删除选中的维修人员？', function (index) {
                    var ids = [];
                    data.forEach(function(item) {
                        ids.push(item.id);
                    });
                    // 这里可以实现批量删除，暂时只删除第一个
                    $.post("/repairMan/delete", {id: ids[0]}, function (res) {
                        if (res.code === 0) {
                            layer.msg("删除成功");
                            table.reload('repairManTable');
                        } else {
                            layer.msg(res.msg || "删除失败");
                        }
                    });
                    layer.close(index);
                });
            }
        });

        // 行内按钮监听 - 编辑/删除
        table.on('tool(repairManTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 隐藏ID字段（编辑时不显示）
                $("#repairManId-laylabel").hide();
                $("#repairManName-laylabel,#repairManPhone-laylabel,#repairManIdCard-laylabel").show();
                
                form.val("editRepairManForm", data);
                index = layer.open({
                    title: '编辑维修人员',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['500px', '400px'],
                    content: $("#editRepairMan")
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该维修人员？', function (index) {
                    $.post("/repairMan/delete", {id: data.id}, function (res) {
                        if (res.code === 0) {
                            layer.msg("删除成功");
                            obj.del();
                        } else {
                            layer.msg(res.msg || "删除失败");
                        }
                    });
                    layer.close(index);
                });
            }
        });

        // 表单提交监听
        form.on('submit(saveRepairManBtn)', function (data) {
            var field = data.field;
            var url = field.id ? "/repairMan/update" : "/repairMan/add";
            
            // 如果是编辑模式，需要构造UpdateRepairManDTO
            var postData = field.id ? {
                id: field.id,
                name: field.name,
                phone: field.phone,
                idCard: field.idCard
            } : field;
            
            $.post(url, postData, function (res) {
                if (res.code === 0) {
                    layer.msg(field.id ? "更新成功" : "添加成功");
                    layer.closeAll();
                    table.reload('repairManTable');
                } else {
                    layer.msg(res.msg || (field.id ? "更新失败" : "添加失败"));
                }
            });
            return false;
        });
    });
</script>
</body>
</html> 